# -*- coding:utf-8 -*-

import random

from django.core.mail import send_mail

from mxonline.settings import EMAIL_FROM
from users.models import EmailVerifyRecord


__author__ = 'maruimin'


def random_str(str_length=8):
    # 生成一段随机字符串
    str = ''
    char_list = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789'
    for i in range(str_length):
        str = str + char_list[random.randint(0, len(char_list)-1)]
    return str


def send_register_email(email, send_type='register'):
    email_record = EmailVerifyRecord()
    if send_type == 'update_email':
        random_code = random_str(4)
    else:
        random_code = random_str(16)
    email_record.code = random_code
    email_record.email = email
    email_record.send_type = send_type
    email_record.save()

    email_title = ''
    email_body = ''

    if send_type == 'register':
        email_title = '慕学网注册链接激活'
        email_body = '请点击下面链接激活你的账户：http://127.0.0.1:8000/active/{}'.format(random_code)

        send_status = send_mail(email_title, email_body, EMAIL_FROM, [email])
        if send_status:
            pass

    elif send_type == 'forget':
        email_title = '慕学网帐号激活'
        email_body = '请点击下面链接重置密码：http://127.0.0.1:8000/reset/{}'.format(random_code)

        send_status = send_mail(email_title, email_body, EMAIL_FROM, [email])
        if send_status:
            pass

    elif send_type == 'update_email':
        email_title = '慕学网更换邮箱验证码'
        email_body = '您的验证码为{0},请严格区分大小写！'.format(random_code)

        send_status = send_mail(email_title, email_body, EMAIL_FROM, [email])
        if send_status:
            pass
